package com.knife.bbs.business;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.knife.bbs.object.BBSDir;
import com.knife.bbs.object.BBSDoc;

public class BBSDirService extends DAOSupportService {
	private static BBSDirService singleton=new BBSDirService();
	public static BBSDirService getInstance()
	{
		return singleton;
	}
	
	public List<BBSDir> getRootDirs()
	{
		List<Object> obj=this.dao.query(com.knife.bbs.domain.BBSDir.class,"status>=0 and parentSn is null or (parentSn='') order by sequence");
		//System.out.println("获取到的频道数是:"+obj.size());
		return changeListType(obj);
	}
	
	public List<BBSDir> getChildDirs(BBSDir dir)
	{
		String scope="parentSn=? order by sequence";
		java.util.Collection<Object> paras=new java.util.ArrayList<Object>();
		paras.add(dir.getSn());
		return changeListType(this.dao.query(com.knife.bbs.domain.BBSDir.class,scope,paras));
	}
	
	public BBSDir getParentDir(BBSDir dir){
		return getBBSDirBySN(dir.getParentsn());
	}
	
	public int todayNum(String dirSn){
		int num=0;
		try{
			java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(
					"yyyy-MM-dd");
			num=Integer.parseInt(this.dao.uniqueResult("select count(*) from BBSDoc where dirSn='"+dirSn+"' and (parentId is null or (parentId='')) and status>=0 and inputTime>='"
						+ dateFormat.format(new Date()) + "'").toString());
		}catch(Exception e){
		}
		return num;
	}
	
	public int messageNum(String dirSn)
	{
		int num=0;
		try{
			num=Integer.parseInt(this.dao.uniqueResult("select count(*) from BBSDoc where dirSn='"+dirSn+"' and (parentId is null or (parentId='')) and status>=0").toString());
		}catch(Exception e){
		}
		return num;
	}
	
	public int replyNum(String dirSn)
	{
		int num=0;
		try{
			num=Integer.parseInt(this.dao.uniqueResult("select count(*) from BBSDoc where dirSn='"+dirSn+"' and not(parentId is null or (parentId='')) and status>=0").toString());
		}catch(Exception e){
		}
		return  num;
	}
	
	public BBSDir getBBSDir(String cid)
	{
		com.knife.bbs.domain.BBSDir dir = (com.knife.bbs.domain.BBSDir)this.dao.get(com.knife.bbs.domain.BBSDir.class,cid);
		if(dir!=null){
			return new BBSDir(dir); 
		}else{
			return null;
		}
	}
	
	public BBSDir getBBSDirBySN(String sn)
	{
		com.knife.bbs.domain.BBSDir dir = (com.knife.bbs.domain.BBSDir)this.dao.getBy(com.knife.bbs.domain.BBSDir.class,"sn",sn);
		if(dir!=null){
			return new BBSDir(dir); 
		}else{
			return null;
		}
	}
	
	public List queryBBSDir(String scope)
	{		
		return this.dao.query(com.knife.bbs.domain.BBSDir.class,scope);	
	}
	
	public BBSDoc getDirLastDoc(BBSDir dir)
	{
		java.util.Collection paras=new java.util.ArrayList();
		paras.add(dir.getSn());
		java.util.List list=this.dao.query(com.knife.bbs.domain.BBSDir.class,"dirSn=? and status>=0 order by inputTime desc",paras,0,1);
		BBSDoc doc=null;
		if(list!=null && list.size()>0)doc=(BBSDoc)list.get(0);
		return doc;
	}
	
	public BBSDoc getDirLastTopic(String dirSn)
	{
		java.util.Collection<Object> paras=new java.util.ArrayList<Object>();
		paras.add(dirSn);
		java.util.List<Object> list = this.dao.query(com.knife.bbs.domain.BBSDoc.class,"dirSn=? and status>=0 and (parentId is null or (parentId='')) order by inputTime desc",paras,0,1);
		com.knife.bbs.domain.BBSDoc doc=null;
		if(list!=null && list.size()>0)doc=(com.knife.bbs.domain.BBSDoc)list.get(0);
		if(doc!=null){
			return new BBSDoc(doc);
		}else{
			return null;
		}
	}
	
	public List<BBSDoc> getDirLastTopics(String dirSn,int num)
	{
		List<BBSDoc> results=new ArrayList<BBSDoc>();
		java.util.Collection<Object> paras=new java.util.ArrayList<Object>();
		paras.add(dirSn);
		java.util.List<Object> list = this.dao.query(com.knife.bbs.domain.BBSDoc.class,"dirSn=? and status>=0 and (parentId is null or (parentId='')) order by inputTime desc",paras,0,num);
		for(Object adoc:list){
			com.knife.bbs.domain.BBSDoc rdoc=(com.knife.bbs.domain.BBSDoc)adoc;
			BBSDoc doc = new BBSDoc(rdoc);
			results.add(doc);
		}
		return results;
		/*
		com.knife.bbs.domain.BBSDoc doc=null;
		if(list!=null && list.size()>0)doc=(com.knife.bbs.domain.BBSDoc)list.get(0);
		if(doc!=null){
			return new BBSDoc(doc);
		}else{
			return null;
		}*/
	}
	
	public List<BBSDir> changeListType(List<Object> objs){
		List<BBSDir> results = new ArrayList<BBSDir>();
		if(objs!=null){
			if(objs.size()>0){
				for (Object newobj : objs) {
					com.knife.bbs.domain.BBSDir robj = (com.knife.bbs.domain.BBSDir) newobj;
					BBSDir tobj = new BBSDir(robj);
					results.add(tobj);
				}
			}
		}
		return results;
	}
}
